/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.commands.request;

import com.bionic.commands.common.ICommand;
import com.bionic.dao.ReqestDAO;
import com.bionic.entity.Currency;
import com.bionic.entity.Reqest;
import com.bionic.entity.ReqestStatus;
import com.bionic.entity.ReqestType;
import com.bionic.entity.Users;
import com.bionic.manager.Config;
import java.io.IOException;
import java.math.BigDecimal;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Artem
 */
public class CRequestInsert implements ICommand  {
    

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null; //  Config.getInstance().getProperty(Config.LOGIN);

        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("currentUser");

        if ((user!=null)&&(user.getId() != null)) {
            ReqestDAO requestDAO = new ReqestDAO();
            
            // Заполняем данные 
            Reqest someReqest = new Reqest();
            someReqest.setAmountFrom(BigDecimal.valueOf(Double.parseDouble(request.getParameter("amount"))));
            
            Currency currencyFrom =  new Currency(Integer.parseInt(request.getParameter("currency_from")));
            someReqest.setCurrencyFromId(currencyFrom);
            
            Currency currencyTo =  new Currency(Integer.parseInt(request.getParameter("currency_to")));
            someReqest.setCurrencyToId(currencyTo);
            
            someReqest.setReqestTypeId(new ReqestType(1));
            someReqest.setStatusId(new ReqestStatus(1));
            someReqest.setUserId(user);
            System.out.println("someReqest");
            
            Integer rowUpdate = requestDAO.insert(someReqest);
            if (rowUpdate > 0) {
                request.setAttribute("update_confirm", "Request confirm");
            } else {
                request.setAttribute("update_confirm", "NO");
            }

            
   
            page = Config.getInstance().getProperty(Config.REQUEST_EDIT);
        } else {
            page = Config.getInstance().getProperty(Config.LOGIN);
        }

        return page;
    }
    
}
